package com.itheima.d4_synchronized_code;


public class Ticket implements Runnable {
    private int ticket = 100;
//实例方法是this
    //类方法是.class
    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                if (ticket <= 0) {
                    break;
                } else {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    ticket--;
                    System.out.println(Thread.currentThread().getName() + "卖出一张，还剩" + ticket + "张票");
                }
            }
        }
    }
    synchronized public void sellTicket() {
        if (ticket <= 0) {
            return;
        } else {
            ticket--;
            System.out.println(Thread.currentThread().getName() + "在卖票" + ticket);
        }
    }
}

